package com.mybatisplus.demo.android.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mybatisplus.demo.android.dao.FileMapper;
import com.mybatisplus.demo.android.enmu.ApiResponse;
import com.mybatisplus.demo.android.enmu.ResultCode;
import com.mybatisplus.demo.android.model.FileInfo;
import com.mybatisplus.demo.android.model.User;
import com.mybatisplus.demo.android.service.FileService;
import com.mybatisplus.demo.android.viewobject.FileVo;
import com.mybatisplus.demo.android.viewobject.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class FileServiceimpl implements FileService {

    @Autowired
    private FileMapper fileMapper;//与dao层进行交互

    @Override
    public ApiResponse addFiles(List<FileInfo> fileInfos) {

        ApiResponse apiResponse = new ApiResponse();

        for (FileInfo fileInfo : fileInfos) {
            FileInfo fileInfo1 = new FileInfo();
            fileInfo1.setFile_path(fileInfo.getFile_path().trim());
            //查询这条地址是否存在
            QueryWrapper<FileInfo> queryWrapper = new QueryWrapper<>(fileInfo1);

            if (null == fileMapper.selectOne(queryWrapper)) {

                fileInfo.setCreate_date("" + LocalDateTime.now());
                fileInfo.setUpdate_date("" + LocalDateTime.now());

                //插入用户
                int i = fileMapper.insert(fileInfo);
                if (i > 0) {
                    apiResponse.setResultCode(ResultCode.SUCCESS);
                } else {
                    apiResponse.setResultCode(ResultCode.ERROR);
                }
            } else {
                //如果存在则更新这条数据信息
                //不写queryWrapper条件 会变更全部
                UpdateWrapper fileUpdateWrapper = new UpdateWrapper();
                fileUpdateWrapper.eq("file_path", fileInfo.getFile_path().trim()); //不写queryWrapper条件 会变更全部

                fileInfo.setUpdate_date("" + LocalDateTime.now());
                int i = fileMapper.update(fileInfo, fileUpdateWrapper);
                if (i > 0) {
                    apiResponse.setResultCode(ResultCode.SUCCESS);
                } else {
                    apiResponse.setResultCode(ResultCode.ERROR);
                }
            }
        }
        if (apiResponse.getCode() == 200) {
            apiResponse.setData(convertFromModel(fileInfos));
        }

        return apiResponse;
    }


    @Override
    public ApiResponse deleteFile(List<String> filePath, String user_id) {
        return null;
    }

    @Override
    public ApiResponse queryFile(String user_id) {


        return null;
    }

    @Override
    public ApiResponse queryModuleFile(String module) {


        return null;
    }


    //将核心模型转化为UI可用模型
    private List<FileVo> convertFromModel(List<FileInfo> fileInfos) {
        if (fileInfos.size() == 0) {
            return null;
        }
        List<FileVo> list = new ArrayList<>();
        for (FileInfo fileInfo : fileInfos) {
            FileVo fileVo = new FileVo();
            BeanUtils.copyProperties(fileInfo, fileVo);
            list.add(fileVo);
        }

        return list;
    }

}
